<?php
declare (strict_types=1);

namespace app\admin\controller\base;

use app\admin\service\base\AdminService;
use app\BaseController;
use think\facade\Request;
use think\response\Json;

/**
 * 管理员权限中心
 */
class Admin extends BaseController
{
    /**
     * 管理员登录
     * @param AdminService $adminService
     * @return Json
     */
    public function login(AdminService $adminService): Json
    {
        $params['phone'] = Request::param('phone/d', 0);
        $params['password'] = Request::param('password/s', '');
        return $adminService->login($params['phone'], $params['password'], 'admin');
    }

    /**
     * 导入教师
     * @return Json
     */
    public function import(AdminService $adminService): Json
    {
        return $adminService->import([]);
    }

    /**
     * 管理员退出
     * @return Json
     */
    public function logout(AdminService $adminService): Json
    {
        return $adminService->logout('admin');
    }

    /**
     * 获取管理员信息
     * @param AdminService $adminService
     * @return Json
     */
    public function getInfo(AdminService $adminService): Json
    {
        return $adminService->getInfo();
    }

    /**
     * 管理员列表
     * @param AdminService $adminService
     * @return Json
     */
    public function list(AdminService $adminService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['name'] = Request::param('name/s');
        $params['phone'] = Request::param('phone/d');
        $params['enable'] = Request::param('enable/d');
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $adminService->list($params);
    }

    /**
     * 添加管理员
     * @param AdminService $adminService
     * @return Json
     */
    public function add(AdminService $adminService): Json
    {
        $params['name'] = Request::param('name/s');
        $params['phone'] = Request::param('phone/d');
        $params['password'] = Request::param('password/s');
        $params['special_id'] = Request::param('special_id/d');
        $params['desc'] = Request::param('desc/s', '');
        $params['duties_id'] = Request::param('duties_id/d', '');
        $params['mobile'] = Request::param('mobile/s', '');
        return $adminService->add($params);
    }

    /**
     * 编辑管理员
     * @param AdminService $adminService
     * @return Json
     */
    public function edit(AdminService $adminService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['name'] = Request::param('name/s');
        $params['phone'] = Request::param('phone/d');
        $params['desc'] = Request::param('desc/s', '');
        $params['duties_id'] = Request::param('duties_id/d', '');
        $params['special_id'] = Request::param('special_id/d', '');
        $params['mobile'] = Request::param('mobile/s', '');
        return $adminService->edit($params);
    }

    /**
     * 删除管理员
     * @param AdminService $adminService
     * @return Json
     */
    public function del(AdminService $adminService): Json
    {
        $id = Request::param('id/d', 0);
        return $adminService->del($id);
    }

    /**
     * 禁用或启用管理员
     * @param AdminService $adminService
     * @return Json
     */
    public function enable(AdminService $adminService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['enable'] = Request::param('enable/d', 2);
        return $adminService->enable($params['id'], $params['enable']);
    }

    /**
     * 管理员修改密码
     * @param AdminService $adminService
     * @return Json
     */
    public function editPwd(AdminService $adminService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['new_password'] = Request::param('new_password/s', '');
        return $adminService->editPwd($params['id'], $params['new_password']);
    }

    /**
     * 管理员修改头像
     * @param AdminService $adminService
     * @return Json
     */
    public function modifyAvatar(AdminService $adminService): Json
    {
        return $adminService->modifyAvatar();
    }


}
